﻿using System;
using System.Windows.Input;

public class RelayCommand : ICommand
{
	private readonly Action _execute;
	private readonly Func<bool> _canExecute;

	// 构造函数
	public RelayCommand(Action execute, Func<bool> canExecute = null)
	{
		_execute = execute ?? throw new ArgumentNullException(nameof(execute));
		_canExecute = canExecute;
	}

	// 判断命令是否可以执行
	public bool CanExecute(object parameter)
	{
		return _canExecute == null || _canExecute();
	}

	// 执行命令
	public void Execute(object parameter)
	{
		_execute();
	}

	// 事件用于通知命令的可执行状态变化
	public event EventHandler CanExecuteChanged;

	// 手动触发 CanExecuteChanged 事件
	public void RaiseCanExecuteChanged()
	{
		CanExecuteChanged?.Invoke(this, EventArgs.Empty);
	}
}
